package cn.miju.ser.alipay.controller;

import cn.miju.ser.alipay.service.AlipayService;
import cn.miju.ser.alipay.vo.*;
import cn.miju.ser.framework.exception.ApiException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <pre>
 * 平台支付宝支付开放接口
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-02-28 22:08
 */
@Api(tags = "支付宝支付")
@RestController
@RequestMapping("/alipay")
public class AlipayController {
    private final AlipayService service;

    public AlipayController(AlipayService service) {
        this.service = service;
    }

    /**
     * 扫码支付下单
     * @param inVO {@link AlipayCreateQrcodeOrderInVO}
     * @return {@link AlipayCreateQrcodeOrderOutVO}
     */
    @ApiOperation("扫码支付下单")
    @ApiOperationSupport(order = 1, author = "zhuming")
    @PostMapping("/createQrcodeOrder")
    public AlipayCreateQrcodeOrderOutVO createQrcodeOrder(@RequestBody @Validated AlipayCreateQrcodeOrderInVO inVO) {
        return service.createQrcodeOrder(inVO);
    }

    /**
     * 查询订单
     * @param inVO {@link AlipayQueryOrderInVO}
     * @return {@link AlipayQueryOrderOutVO}
     */
    @ApiOperation("查询订单")
    @ApiOperationSupport(order = 2, author = "zhuming")
    @PostMapping("/queryOrder")
    public AlipayQueryOrderOutVO queryOrder(@RequestBody @Validated AlipayQueryOrderInVO inVO) {
        return service.queryOrder(inVO);
    }

    /**
     * 订单退款
     * @param inVO {@link AlipayRefundOrderInVO}
     * @return {@link AlipayRefundOrderOutVO}
     */
    @ApiOperation("订单退款")
    @ApiOperationSupport(order = 3, author = "zhuming")
    @PostMapping("/refundOrder")
    public AlipayRefundOrderOutVO refundOrder(@RequestBody @Validated AlipayRefundOrderInVO inVO) {
        AlipayRefundOrderOutVO outVO = service.refundOrder(inVO);
        if (!outVO.isRefundSuccess()) {
            throw new ApiException(outVO.getFailMessage());
        }
        return outVO;
    }

}
